package com.wendu.oa.service.security.impl;

import com.wendu.oa.common.CurrentUser;
import com.wendu.oa.dao.security.LoginDao;
import com.wendu.oa.dto.LoginDto;
import com.wendu.oa.exception.BusinessException;
import com.wendu.oa.exception.SysException;
import com.wendu.oa.model.User;
import com.wendu.oa.service.security.LoginService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service @Transactional
public class LoginServiceImpl implements LoginService {

    @Resource //等同于@Autowired
    private LoginDao loginDao;

    @Override
    public CurrentUser checkLogin(LoginDto loginDto) {
        User user = null;
        try {
            user = loginDao.findUserByIdAndPwd(loginDto);

        } catch (Exception e) {
           throw new SysException("登录失败！",e);
        }
        if(user == null){
            throw new BusinessException("账号或密码错误！");
        }

        return new CurrentUser(user.getU_id(),user.getU_name());

    }
}
